/*
分离提取Url的参数
    * @name {string} 要提取的属性名key
    * @str  {string} 要分解的属性值 
    * @return {Object} 要提取的值value
 */
export const getUrlParamByName = (name, str) => {
  // debugger
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
  const r = str.substr(1).match(reg);
  if (r != null) return decodeURIComponent(r[2]);
  return null;
};

/*
判断客户端-微信？
 */
export const isWeiXin = () => {
  //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
  let ua = window.navigator.userAgent.toLowerCase();
  //通过正则表达式匹配ua中是否含有MicroMessenger字符串
  // return ua.indexOf('micromessenger') !== -1;
  if (ua.match(/MicroMessenger/i) == "micromessenger") {
    return true;
  } else {
    return false;
  }
  // return (/MicroMessenger/.test(ua));
};

/*
判断客户端-支付宝？
 */
export const isali = () => {
  //判断是否支付宝浏览器打开
  let ua = window.navigator.userAgent.toLowerCase();
  return ua.indexOf("alipay") !== -1;
  // return (/AlipayClient/.test(ua))
};

/*
判断客户端
 */
export const clientType = () => {
  if (isMiniProgram()) {
    return 18;
  } else if (isWeiXin()) {
    return 1;
  } else if (isali()) {
    return 2;
  } else {
    return window.location.hostname.indexOf("wxchart") > -1 ? 1 : 2;
  }
};

/*
设置url参数
*/
export function changeURLArg(url, arg, arg_val) {
  var pattern = arg + "=([^&]*)";
  var replaceText = arg + "=" + arg_val;
  if (url.match(pattern)) {
    var tmp = "/(" + arg + "=)([^&]*)/gi";
    tmp = url.replace(eval(tmp), replaceText);
    return tmp;
  } else {
    if (url.match("[?]")) {
      return url + "&" + replaceText;
    } else {
      return url + "?" + replaceText;
    }
  }
}

// 判断是否在web-view内
export function isMiniProgram() {
  var u = navigator.userAgent;
  if (u.indexOf("miniProgram") != -1) {
    //在微信小程序
    return 1;
  } else {
    return 0;
    //在微信网页
  }
}

// 日期转-年月日
export function formatDate_ZH(d) {
  if (!d) return "";
  d = new Date(d.replace(/-/g, "/"));
  return (
    d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日"
  );
}

// 手机号中间四位转*
export function phoneWithStar(phone) {
  phone = phone.toString();
  return phone.substr(0, 3) + "****" + phone.substr(7);
}
